/*
 * XStatusBar.java, created on Oct 21, 2005
 * 
 * Copyright (c) 2005 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.components;

import java.util.EventObject;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import org.xfc.XApp;
import org.xfc.eventbus.XProgressEvent;
import org.xfc.eventbus.XStatusEvent;
import org.xfc.eventbus.XSubscriber;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;


// TODO: configurable to add components to the status bar

// TODO: fix the issue w/ not revalidating the panel

// TODO: listen to determinate progress events -

// TODO: you should have to deliberately cause this component to start listening to application wide events

// TODO: on the mac platform, we need to leave room for the grow icon / resize area on the right of the status bar


/**
 *
 *
 * @author Devon Carew
 */
public class XStatusBar
	extends JComponent
{
	private XApp app;
	
	/**
	 * 
	 */
	public XStatusBar()
	{
		this(null);
	}
	
	/**
	 * @param app
	 */
	public XStatusBar(XApp app)
	{
		this.app = app;
		
		setBorder(new CompoundBorder(
			XApp.getComponentFactory().createSubtleComponentBorder(), 
			new EmptyBorder(0, 2, 0, 2)
		));
		
		setLayout(new FormLayout("", "12dlu 1px"));
		
		addStatusArea(null);
	}
	
	/**
	 * 
	 * 
	 * @param statusAreaName
	 */
	public void addStatusArea(final String statusAreaName)
	{
		final JLabel status = new JLabel("");
		
		app.getEventBus().subscribe(new XSubscriber() {
			public void handleEvent(EventObject event)
			{
				XStatusEvent statusEvent = (XStatusEvent)event;
				
				if (statusAreaName == null && statusEvent.getStatusAreaName() == null)
					status.setText(statusEvent.getStatusMessage());
				else if (statusAreaName != null && statusAreaName.equals(statusEvent.getStatusAreaName()))
					status.setText(statusEvent.getStatusMessage());
			}
		}, XStatusEvent.class);
		
		FormLayout layout = (FormLayout)getLayout();

		if (statusAreaName == null)
		{
			layout.appendColumn(new ColumnSpec("left:75dlu:grow"));
			add(status, layout.getColumnCount() + ",1");
		}
		else
		{
			addSeparator();
			
			layout.appendColumn(new ColumnSpec("left:default"));
			add(status, layout.getColumnCount() + ",1");
		}
		
		validate();
	}
	
	/**
	 * 
	 */
	public void addProgressBar()
	{
		final JProgressBar progressBar = new JProgressBar();

		app.getEventBus().subscribe(new XSubscriber() {
			public void handleEvent(EventObject event) {
				XProgressEvent progress = (XProgressEvent)event;
				
				progressBar.setIndeterminate(progress.getEventType() == XProgressEvent.INDETERMINATE_START);
			}
		}, XProgressEvent.class);
		
		addSeparator();
		
		FormLayout layout = (FormLayout)getLayout();
		
		layout.appendColumn(new ColumnSpec("center:default"));
		
		add(progressBar, layout.getColumnCount() + ", 1, f, c");
		
		validate();
	}
	
	/**
	 * 
	 * 
	 * @param component
	 */
	public void addComponent(JComponent component)
	{
		addSeparator();
		
		component.setFocusable(false);
		
		FormLayout layout = (FormLayout)getLayout();
		
		layout.appendColumn(new ColumnSpec("default"));
		
		add(component, layout.getColumnCount() + ",1");
		
		validate();
	}
	
	/**
	 * Add a visual separator / spacer to the status bar.
	 */
	private void addSeparator()
	{
		JComponent separator = new JSeparator(SwingConstants.VERTICAL);
		
		FormLayout layout = (FormLayout)getLayout();
		
		layout.appendColumn(new ColumnSpec("10dlu"));
		
		add(separator, layout.getColumnCount() + ",1,c,f");
	}
	
}
